package io.renren.modules.notice.controller;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.common.utils.R;
import io.renren.modules.notice.entity.PsNoticeEntity;
import io.renren.modules.notice.service.PsNoticeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;




/**
 * 通知公告
 * 
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2018-07-19 12:03:21
 */
@RestController
@RequestMapping("/generator/psnotice")
public class PsNoticeController {
	@Autowired
	private PsNoticeService psNoticeService;
	
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("generator:psnotice:list")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);

		List<PsNoticeEntity> psNoticeList = psNoticeService.queryList(query);
		int total = psNoticeService.queryTotal(query);
		
		PageUtils pageUtil = new PageUtils(psNoticeList, total, query.getLimit(), query.getPage());
		
		return R.ok().put("page", pageUtil);
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	@RequiresPermissions("generator:psnotice:info")
	public R info(@PathVariable("id") String id){
		PsNoticeEntity psNotice = psNoticeService.queryObject(id);
		
		return R.ok().put("psNotice", psNotice);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("generator:psnotice:save")
	public R save(@RequestBody PsNoticeEntity psNotice){
		String type = psNotice.getType();
		int total = psNoticeService.selectTotal(type);
		if(total > 0){
			return R.error(1,"通知类型已经存在！");
		}
		psNoticeService.save(psNotice);
		
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("generator:psnotice:update")
	public R update(@RequestBody PsNoticeEntity psNotice){
		String type = psNotice.getType();
		String selectType = psNoticeService.queryObject(psNotice.getId().toString()).getType();
		if(type.equals(selectType)){
			psNoticeService.update(psNotice);
		}else{
			int total = psNoticeService.selectTotal(type);
			if(total > 0 ){
				return R.error(1,"通知类型已经存在！");
			}
		}
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("generator:psnotice:delete")
	public R delete(@RequestBody String[] ids){
		psNoticeService.deleteBatch(ids);
		
		return R.ok();
	}
	
}
